﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using BitmapTagger;
using BrainTechLLC;
using BrainTechLLC.FilterExtensions;
using BrainTechLLC.ThreadSafeObjects;
using BrainTechLLC.WPF;
using ImageAppDataModels;
using System.Collections.Specialized;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using System.Reflection;
using System.Collections.ObjectModel;

namespace ImageAppViewModels
{
    public class GroupViewModel : GenericViewModelBase<GroupViewModel, Group>, INotifyPropertyChanged
    {
        protected GroupCollectionViewModel _ParentCollection;
        protected ObservableCollection<FileImageGroupViewModel> _FileImages;
        protected ThreadSafeLookup<string, FileImageGroupViewModel> _FileImagesByFileName = new ThreadSafeLookup<string,FileImageGroupViewModel>();
        protected object _lockObject = new object();
        protected bool _IsSelected;


        public ObservableCollection<FileImageGroupViewModel> FileImages
        {
            get
            {
                if (_FileImages == null)
                {
                    lock (_lockObject)
                    {
                        if (_FileImages == null)
                        {
                            var collection = new ObservableCollection<FileImageGroupViewModel>();
                            collection.CollectionChanged += new NotifyCollectionChangedEventHandler(collection_CollectionChanged);
                            _FileImages = collection;
                        }
                    }
                }

                return _FileImages;
            }
        }

        public int Count
        {
            get { return FileImages.Count; }
        }

        public string GroupName
        {
            get { return Item.GroupName; }
            set { Item.GroupName = value; }
        }

        public void AddFileImageToGroup(FileImageGroupViewModel vm)
        {
            vm.PropertyChanged += new PropertyChangedEventHandler(vm_PropertyChanged);
            FileImages.Add(vm);
            _FileImagesByFileName.Add(vm.FileName, vm);
            _ParentCollection.OnCurrentlySelectedGroupsInListboxChanged();
        }

        void vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            RaisePropertyChanged(e.PropertyName);
        }

        void collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // TODO
            OnPropertyChanged("Count");
        }

        public GroupViewModel()
        {
        }

        public GroupViewModel(Group g, GroupCollectionViewModel parentCollection)
            : base(g)
        {
            _ParentCollection = parentCollection;
        }

        public void RemoveFileImageFromGroup(FileImageGroupViewModel o)
        {
            o.PropertyChanged -= vm_PropertyChanged;
            FileImages.Remove(o);
            _FileImagesByFileName.Remove(o.FileName);
        }

        public void ClearFileImageFromGroup()
        {
            for (int n = FileImages.Count - 1; n >= 0; n--)
            {
                var fi = FileImages[n];
                fi.PropertyChanged -= vm_PropertyChanged;                
            }

            FileImages.Clear();
            _FileImagesByFileName.Clear();
        }

        public FileImageGroupViewModel FindFindImage(string fileName)
        {
            return _FileImagesByFileName[fileName];            
        }

        public bool IsSelected
        {
            get { return _IsSelected; }
            set
            {
                if (_IsSelected != value)
                {
                    _IsSelected = value;
                    OnPropertyChanged("IsSelected");
                }
            }
        }

    }
}
